﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Web;
using System.Web.UI.WebControls;
using System.Web.UI;

namespace Library
{
	public class ExcelHelper
	{
		public static void GridExportToExcel(GridView gdvShow, Page p, string excelName)
		{
			// GridExportToExcel(gdvShow, p, excelName,"utf-8");
			GridExportToExcel(gdvShow, p, excelName, System.Text.Encoding.Default.BodyName);
		}

		public static void GridExportToExcel(GridView gdvShow,Page p,string excelName,string charset)
		{
			
			p.Response.Clear();
			p.Response.Buffer = false;
			if (charset == string.Empty)
			{
				//p.Response.Charset = "utf-8";System.Text.Encoding.Default.BodyName
				p.Response.Charset = System.Text.Encoding.Default.BodyName;
			}
			else
			{
				p.Response.Charset = charset;
			}
			p.Response.AppendHeader("Content-Disposition", "attachment;filename=" + excelName + ".xls");
			p.Response.ContentEncoding = System.Text.Encoding.GetEncoding(charset);
			p.Response.ContentType = "application/ms-excel"; 
			p.EnableViewState = false;
			System.IO.StringWriter oStringWriter = new System.IO.StringWriter();
			System.Web.UI.HtmlTextWriter oHtmlTextWriter = new System.Web.UI.HtmlTextWriter(oStringWriter);
			gdvShow.RenderControl(oHtmlTextWriter);
			p.Response.Write(oStringWriter.ToString());
			p.Response.End();
		}


	}
}
